package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
	@Autowired
	private OrderService orderService;

	@PostMapping("/submit")
	public R<String> submit(@RequestBody Orders orders){
		log.info("订单数据：{}",orders);
		orderService.submit(orders);
		return R.success("下单成功");
	}
	@GetMapping("/userPage")
	private R<Page<Orders>> userPage(Integer page, Integer paseSize){
		Page<Orders> ordersPage = new Page<>(page,paseSize);

		LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();

		queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
		queryWrapper.orderByDesc(Orders::getOrderTime);
		List<Orders> list = orderService.list(queryWrapper);

		orderService.page(ordersPage,queryWrapper);
		return R.success(ordersPage);
	}
}